package moa.sequencestreams.generator;

import java.util.Random;

import org.apache.commons.math.random.RandomDataImpl;


public class StringSetIter {

	
	private	  StringSet strset;
	private	  int last_pat;	// if -ve, unget_pat() was called
	
	public	  StringSetIter(StringSet str_set) 
	{
		strset= str_set;
		last_pat=0;
	}
	
	public ArrayString get_pat()
	{
		  float r;
		  int i = 0;

		  if (last_pat < 0) {
		    last_pat = -last_pat;
		    if (strset.getTax() == null)
		      return strset.get_pat(last_pat);
		    else
		      return strset.specialize(last_pat);
		  }
		  
		  
		  Random rd = new Random();
		  // find the desired pattern using cum_prob table
		  r = rd.nextFloat();
		  
		  i = (int) (r * strset.getNpats());
		  
		  if (i == strset.getNpats())
		    i--;
		  while ( i < (strset.getNpats()-1) && r > strset.getCum_prob()[i] )
		    i++;
		  while ( i > 0 && r < strset.getCum_prob()[i-1] )
		    i--;
		  last_pat = i;

		  if (strset.getTax() == null)
		    return strset.get_pat(i);
		  else
		    return strset.specialize(i);
	}
	
	public	  void unget_pat()
	{
		// the last pattern is put back in the sequence
	}
}
